package com.getpebble.android.common.framework;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import com.getpebble.android.common.core.trace.Trace;
import com.google.gson.Gson;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class MultiProcPreferences implements SharedPreferences {
    private static final String TAG = MultiProcPreferences.class.getSimpleName();
    private static PreferenceCache sPreferenceCache;
    private final Editor mEditor = new MultiProcPreferencesEditor();
    private final String mName;

    class MultiProcPreferencesEditor implements Editor {
        private boolean mClearBeforeApplying = false;
        private final ContentValues mPreferencesAdded = new ContentValues();
        private final Set<String> mPreferencesRemoved = new HashSet();

        MultiProcPreferencesEditor() {
        }

        public Editor putString(String key, String value) {
            synchronized (this) {
                this.mPreferencesAdded.put(key, value);
            }
            return this;
        }

        public Editor putStringSet(String key, Set<String> values) {
            synchronized (this) {
                this.mPreferencesAdded.put(key, new Gson().toJson((Object) values));
            }
            return this;
        }

        public Editor putInt(String key, int value) {
            synchronized (this) {
                this.mPreferencesAdded.put(key, Integer.valueOf(value));
            }
            return this;
        }

        public Editor putLong(String key, long value) {
            synchronized (this) {
                this.mPreferencesAdded.put(key, Long.valueOf(value));
            }
            return this;
        }

        public Editor putFloat(String key, float value) {
            synchronized (this) {
                this.mPreferencesAdded.put(key, Float.valueOf(value));
            }
            return this;
        }

        public Editor putBoolean(String key, boolean value) {
            synchronized (this) {
                this.mPreferencesAdded.put(key, Boolean.valueOf(value));
            }
            return this;
        }

        public Editor remove(String key) {
            synchronized (this) {
                if (this.mPreferencesAdded.containsKey(key)) {
                    this.mPreferencesAdded.remove(key);
                }
                this.mPreferencesRemoved.add(key);
            }
            return this;
        }

        public Editor clear() {
            synchronized (this) {
                this.mPreferencesAdded.clear();
                this.mPreferencesRemoved.clear();
                this.mClearBeforeApplying = true;
            }
            return this;
        }

        public boolean commit() {
            apply();
            return true;
        }

        public void apply() {
            synchronized (this) {
                Trace.verbose(MultiProcPreferences.TAG, "Applying pref edits commit(" + MultiProcPreferences.this.mName + ")");
                MultiProcPreferences.sPreferenceCache.commit(MultiProcPreferences.this.mName, this.mPreferencesAdded, this.mPreferencesRemoved, Boolean.valueOf(this.mClearBeforeApplying));
                this.mPreferencesAdded.clear();
                this.mPreferencesRemoved.clear();
                this.mClearBeforeApplying = false;
            }
        }
    }

    public MultiProcPreferences(Context context, String name) {
        sPreferenceCache = getPreferenceCache(context);
        this.mName = name;
    }

    public Map<String, ?> getAll() {
        return sPreferenceCache.getAll(this.mName);
    }

    public String getString(String key, String defValue) {
        return sPreferenceCache.getString(this.mName, key, defValue);
    }

    public Set<String> getStringSet(String key, Set<String> defValues) {
        return sPreferenceCache.getStringSet(this.mName, key, defValues);
    }

    public int getInt(String key, int defValue) {
        return sPreferenceCache.getInt(this.mName, key, defValue);
    }

    public long getLong(String key, long defValue) {
        return sPreferenceCache.getLong(this.mName, key, defValue);
    }

    public float getFloat(String key, float defValue) {
        return sPreferenceCache.getFloat(this.mName, key, defValue);
    }

    public boolean getBoolean(String key, boolean defValue) {
        return sPreferenceCache.getBoolean(this.mName, key, defValue);
    }

    public boolean contains(String key) {
        return sPreferenceCache.contains(this.mName, key);
    }

    public Editor edit() {
        return this.mEditor;
    }

    public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
        sPreferenceCache.addListener(listener, this);
    }

    public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
        sPreferenceCache.removeListener(listener);
    }

    private static synchronized PreferenceCache getPreferenceCache(Context context) {
        PreferenceCache preferenceCache;
        synchronized (MultiProcPreferences.class) {
            if (sPreferenceCache == null) {
                sPreferenceCache = new PreferenceCache(context.getContentResolver());
                long startTime = System.currentTimeMillis();
                Trace.debug(TAG, "Initializing preference cache");
                sPreferenceCache.init();
                Trace.debug(TAG, String.format("Done initializing prefs; took <%d> ms", new Object[]{Long.valueOf(System.currentTimeMillis() - startTime)}));
            }
            preferenceCache = sPreferenceCache;
        }
        return preferenceCache;
    }

    public static synchronized void init(Context context) {
        synchronized (MultiProcPreferences.class) {
            getPreferenceCache(context);
        }
    }

    String getFile() {
        return this.mName;
    }
}
